import api from './api';

export const userService = {
  // 创建用户
  createUser: async (userData) => {
    const response = await api.post('/api/users', userData);
    return response.data;
  },

  // 获取指定用户
  getUser: async (userId) => {
    const response = await api.get(`/api/users/${userId}`);
    return response.data;
  },

  // 获取所有用户
  getAllUsers: async () => {
    const response = await api.get('/api/users');
    return response.data;
  },

  // 更新用户
  updateUser: async (userId, userData) => {
    const response = await api.put(`/api/users/${userId}`, userData);
    return response.data;
  },

  // 删除用户
  deleteUser: async (userId) => {
    const response = await api.delete(`/api/users/${userId}`);
    return response.data;
  },

  // 更新用户角色
  updateUserRole: async (userId, role) => {
    const response = await api.put(`/api/users/${userId}/role`, role);
    return response.data;
  },

  // 更新用户权限
  updateUserPermissions: async (userId, permissions) => {
    const response = await api.put(`/api/users/${userId}/permissions`, permissions);
    return response.data;
  },

  register: async (userData) => {
    const response = await api.post('/api/users/register', userData);
    return response.data;
  },

  login: async (username, password) => {
    const response = await api.post('/api/users/login', null, {
      params: { username, password }
    });
    const token = response.data;
    localStorage.setItem('token', token);
    return token;
  },

  getCurrentUser: async () => {
    const response = await api.get('/api/users/profile');
    return response.data;
  },

  updateProfile: async (userData) => {
    const response = await api.put('/api/users/profile', userData);
    return response.data;
  },

  getUserByUsername: async (username) => {
    const response = await api.get(`/api/users/${username}`);
    return response.data;
  },

  logout: () => {
    localStorage.removeItem('token');
  }
}; 